#!/bin/bash

#
# budo-graph dump-template.sh
# applicationName: ${applicationName!}
# PID: ${PID}
# now: ${now}
#

# 默认PID
PID=${PID}

# 指定PID
if [ $1 ]; then
    PID=$1
fi


# 检查进程是否在
PID_EXIST=$(ps aux | awk '{print $2}' | grep -w $PID)

# 不存在指定ID的进程
if [ ! $PID_EXIST ]; then
    echo "ERROR: APP is not running, PID_EXIST = $PID_EXIST, PID = $PID"
    exit 1
fi

# 拼目录 freemarker $ 转义
DUMP_DIR=`pwd`/_dump_${'$'}{PID}_`date +%Y%m%d`_`date +%H%M%S`
mkdir -p $DUMP_DIR


# 开始干活
echo " INFO: DUMP_DIR=$DUMP_DIR PID=$PID"

echo -e " INFO: start dumping, applicationName = ${applicationName!}, PID = $PID"

echo -e " INFO: 1/16 before jstack"
jstack $PID > $DUMP_DIR/jstack.dump 2>&1
echo -e " INFO: 1/16 after jstack"

echo -e " INFO: 2/16 before jinfo"
jinfo $PID > $DUMP_DIR/jinfo.dump 2>&1
echo -e " INFO: 2/16 after jinfo"

echo -e " INFO: 3/16 before jstat -gcutil"
jstat -gcutil $PID > $DUMP_DIR/jstat-gcutil.dump 2>&1
echo -e " INFO: 3/16 after jstat -gcutil"

echo -e " INFO: 4/16 before jstat -gccapacity"
jstat -gccapacity $PID > $DUMP_DIR/jstat-gccapacity.dump 2>&1
echo -e " INFO: 4/16 after jstat -gccapacity"

echo -e " INFO: 5/16 before jmap"
jmap $PID > $DUMP_DIR/jmap.dump 2>&1
echo -e " INFO: 5/16 after jmap"

echo -e " INFO: 6/16 before jmap -heap"
jmap -heap $PID > $DUMP_DIR/jmap-heap.dump 2>&1
echo -e " INFO: 6/16 after jmap -heap"

echo -e " INFO: 7/16 before jmap -histo"
jmap -histo $PID > $DUMP_DIR/jmap-histo.dump 2>&1
echo -e " INFO: 7/16 after jmap -histo"

echo -e " INFO: 8/16 before ps -mp"
ps -mp $PID -o THREAD,tid,time | sort -rn > $DUMP_DIR/ps-mp.dump 2>&1
echo -e " INFO: 8/16 after ps -mp"

echo -e " INFO: 9/16 before lsof"
if [ -r /usr/sbin/lsof ]; then
    /usr/sbin/lsof -p $PID > $DUMP_DIR/lsof.dump
    echo -e " INFO: 9/16 after lsof"
else
    echo "ERROR: 9/16 no /usr/usr/sbin/lsof"
fi

echo -e " INFO: 10/16 before netstat -an"
if [ -r /bin/netstat ]; then
    /bin/netstat -an > $DUMP_DIR/netstat.dump 2>&1
    echo -e " INFO: 10/16 after netstat -an"
else
    echo "ERROR: 10/16 no /bin/netstat"
fi

echo -e " INFO: 11/16 before iostat"
if [ -r /usr/bin/iostat ]; then
    /usr/bin/iostat > $DUMP_DIR/iostat.dump 2>&1
    echo -e " INFO: 11/16 after iostat"
else
    echo "ERROR: 11/16 no /usr/bin/iostat"
fi

echo -e " INFO: 12/16 before mpstat"
if [ -r /usr/bin/mpstat ]; then
    /usr/bin/mpstat > $DUMP_DIR/mpstat.dump 2>&1
    echo -e " INFO: 12/16 after mpstat"
else
    echo "ERROR: 12/16 no /usr/bin/mpstat"
fi

echo -e " INFO: 13/16 before vmstat"
if [ -r /usr/bin/vmstat ]; then
    /usr/bin/vmstat > $DUMP_DIR/vmstat.dump 2>&1
    echo -e " INFO: 13/16 after vmstat"
else
    echo "ERROR: 13/16 no /usr/bin/vmstat"
fi

echo -e " INFO: 14/16 before free"
if [ -r /usr/bin/free ]; then
    /usr/bin/free -t > $DUMP_DIR/free.dump 2>&1
    echo -e " INFO: 14/16 after free"
else
    echo "ERROR: 14/16 no /usr/bin/free"
fi

echo -e " INFO: 15/16 before sar"
if [ -r /usr/bin/sar ]; then
    /usr/bin/sar > $DUMP_DIR/sar.dump 2>&1
    echo -e " INFO: 15/16 after sar"
else
    echo "ERROR: 15/16 no /usr/bin/sar"
fi

echo -e " INFO: 16/16 before uptime"
if [ -r /usr/bin/uptime ]; then
    /usr/bin/uptime > $DUMP_DIR/uptime.dump 2>&1
    echo -e " INFO: 16/16 after uptime"
else
    echo "ERROR: 16/16 no /usr/bin/uptime"
fi

echo " INFO: DUMP done: applicationName = ${applicationName!}, PID = $PID, DUMP_DIR = $DUMP_DIR"
